﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace Devtm.Parsing
{


    public class Parser
    {


        private System.Collections.Hashtable _cache;


        public Parser(System.Collections.Hashtable cache, IParsingVisitor visitor)
        {
            this._cache = cache;
            this.Visitor = visitor;
        }

        [System.Diagnostics.DebuggerStepThrough]
        public string Evaluate(string vv, XElement xdata)
        {

            string result = string.Empty;

            Lexer l = new Lexer(vv);

            try
            {

                StringBuilder s = new StringBuilder();

                Context c = new Context()
                {
                    //Stream = s, 
                    Cache = _cache,
                    Datas = xdata,
                    Visitor = Visitor,
                };

                ItemBase i = new ItemBase();

                result = i.Parse(l, c, string.Empty);

            }
            catch(Exception e)
            {
                throw new Exception(string.Format("fail to evaluate value : \"{0}\".", l.ResultMatch.Value), e);
            }


            

            return result;

        }

        public IParsingVisitor Visitor { get; set; }

    }

}
